package dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.annotation.Transactional;

import beans.Follow;
import beans.Rate;
import beans.Restaurant;
import beans.User;
@Transactional
public class RateDAO {

	private HibernateTemplate hibernateTemplate;
	public void setSessionFactory(SessionFactory sessionFactory){
		this.hibernateTemplate = new HibernateTemplate(sessionFactory);
	}
	
	public void saveRate(Rate rate){
		this.hibernateTemplate.save(rate);
	}
	public void updateRate(Rate rate){
		this.hibernateTemplate.update(rate);
	}
	public void deleteRate(Rate rate){
		this.hibernateTemplate.delete(rate);
	}
	public List<Rate> GetAllRates(){
		return this.hibernateTemplate.loadAll(Rate.class);
	}
	public Rate findRate(User user, int restaurant_id){
		Rate rate = new Rate();
		for(Rate x: GetAllRates()){
			if((x.getUsername().equalsIgnoreCase(user.getUsername())&&(x.getRestaurant_id()==restaurant_id))){
				rate.setId(x.getId());
				rate.setUsername(x.getUsername());
				rate.setRestaurant_id(x.getRestaurant_id());
				rate.setRate(x.getRate());
				return rate;
			}
		}
		return null;
	}
}
